﻿@using System.Reflection
@using System.Linq.Expressions
@using Whoever.Framework.Base;
@model Whoever.Patterns.Website2.Base.ComboModel
@{
    Type modelType = Model.GetType();
    Type viewModelType = Model.ViewModel.GetType();
    Type entityModelType = Model.ViewModel.EntityType;

    var expParameter = Expression.Parameter(modelType, "m");

    object sourceObject;
    PropertyInfo foreignKeyProperty;

    Expression sourceExpression;
    var modelProperty = modelType.GetProperty("ViewModel");
    var expModelProperty = Expression.Property(expParameter, modelProperty);
    var expAsModelType = Expression.TypeAs(expModelProperty, viewModelType);

    //Si existe la propiedad en el viewModel se reemplaza la original de la entity
    if (viewModelType.GetProperty(Model.PropertyForeignKey.Name) != null)
    {
        sourceObject = Model.ViewModel;
        sourceExpression = expAsModelType;
        foreignKeyProperty = viewModelType.GetProperty(Model.PropertyForeignKey.Name);
    }
    else
    {
        sourceObject = Model.ViewModel.Entity;
        var entityModelProperty = viewModelType.GetProperty("Entity");
        var expEntityModelProperty = Expression.Property(expAsModelType, entityModelProperty);
        var expAsEntityModelType = Expression.TypeAs(expEntityModelProperty, entityModelType);
        sourceExpression = expAsEntityModelType;
        foreignKeyProperty = entityModelType.GetProperty(Model.PropertyForeignKey.Name);
    }
    var prop = Expression.Property(sourceExpression, Model.PropertyForeignKey.Name);

    var list = (IEnumerable<BusinessEntity>)Model.PropertyList.GetValue(Model.ViewModel, new object[] { });
    SelectList selectList;

    var sourceForeignValue = foreignKeyProperty.GetValue(sourceObject, new object[] { });

    if (sourceForeignValue != null)
    {
        selectList = new SelectList(list, Model.PropertyListKey, Model.PropertyListValue, sourceForeignValue);
    }
    else
    {
        selectList = new SelectList(list, Model.PropertyListKey, Model.PropertyListValue);
    }
}
@{
    //Seria bueno encontrar una mejor solucion a esto. Lo complicado es la definicion de la expression dependiendo del type de la property dinamicamente
    if (foreignKeyProperty.PropertyType == typeof(int))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.ComboModel, int>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.DropDownListFor(expLambda, selectList, "Seleccione", Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (foreignKeyProperty.PropertyType == typeof(int?))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.ComboModel, int?>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.DropDownListFor(expLambda, selectList, "Seleccione", Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (foreignKeyProperty.PropertyType == typeof(string))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.ComboModel, string>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.DropDownListFor(expLambda, selectList, "Seleccione", Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (foreignKeyProperty.PropertyType == typeof(decimal))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.ComboModel, decimal>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.DropDownListFor(expLambda, selectList, "Seleccione", Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (foreignKeyProperty.PropertyType == typeof(decimal?))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.ComboModel, decimal?>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.DropDownListFor(expLambda, selectList, "Seleccione", Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (foreignKeyProperty.PropertyType == typeof(double))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.ComboModel, double>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.DropDownListFor(expLambda, selectList, "Seleccione", Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (foreignKeyProperty.PropertyType == typeof(double?))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.ComboModel, double?>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.DropDownListFor(expLambda, selectList, "Seleccione", Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (foreignKeyProperty.PropertyType == typeof(DateTime))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.ComboModel, DateTime>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.DropDownListFor(expLambda, selectList, "Seleccione", Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (foreignKeyProperty.PropertyType == typeof(DateTime?))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.ComboModel, DateTime?>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.DropDownListFor(expLambda, selectList, "Seleccione", Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }

    if (foreignKeyProperty.PropertyType == typeof(bool))
    {
        var expLambda = Expression.Lambda<Func<Whoever.Patterns.Website2.Base.ComboModel, bool>>(prop, expParameter);
    <text>
    <div class="control-group">
        <div class="control-label">
            @Html.LabelFor(expLambda)
        </div>
        <div class="controls">
            @Html.DropDownListFor(expLambda, selectList, "Seleccione", Model.HTMLAttributes)
            @Html.ValidationMessageFor(expLambda)
        </div>
    </div>
    </text>
    }   
}
